package com.mmall.utils;

import org.apache.commons.lang.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.util.Date;

/**
 * Created by chenxu on 17/7/27.
 */
public class DateTimeUtil {
    private static final String STANDARD_FORMAT="yyyy-MM-dd HH:mm:ss";

    public static Date strToDate(String dateTimeStr, String format){
        DateTimeFormatter dateTimeFormat = DateTimeFormat.forPattern(format);
        DateTime dateTime = dateTimeFormat.parseDateTime(dateTimeStr);
        return dateTime.toDate();
    }

    public static Date strToDate(String dateTimeStr){
        DateTimeFormatter dateTimeFormat = DateTimeFormat.forPattern(STANDARD_FORMAT);
        DateTime dateTime = dateTimeFormat.parseDateTime(dateTimeStr);
        return dateTime.toDate();
    }

    public static String dateToStr(Date date, String formatStr){
        if ( date ==null){
            return StringUtils.EMPTY;
        }
        DateTime dateTime = new DateTime(date);
        return dateTime.toString(formatStr);
    }

    public static String dateToStr(Date date){
        if ( date ==null){
            return StringUtils.EMPTY;
        }
        DateTime dateTime = new DateTime(date);
        return dateTime.toString(STANDARD_FORMAT);
    }

    public static void main(String[] args){
        DateTimeUtil util =new DateTimeUtil();
        Date date =new Date(System.currentTimeMillis());
        String dateStr = dateToStr(date, "yyyy-MM-dd HH:mm:ss");
        System.out.println("dateStr " + dateStr);

        Date nowDate = strToDate(dateStr,"yyyy-MM-dd HH:mm:ss");
        String timeStr = nowDate.toGMTString();
        System.out.println("timeStr: " + timeStr);
    }
}
